#include "PhDSave.h"

#include <QFile>
#include <QTextStream>
#include <QDataStream>
#include <cstdlib>

using namespace std;

PhDSave::PhDSave()
{

}

PhDSave::~PhDSave()
{

}

int PhDSave::save(Output output,Layer mag)
{
  QFile filex,filey,filez;
  QTextStream phdfilex(&filex);
  QTextStream phdfiley(&filey);
  QTextStream phdfilez(&filez);
  QString filename;
  QTextStream cout(stdout);
  QTextStream cin(stdin);
  
  //mz matrix
  filename=output.getPhDFile();
  filename.append("_mz.txt");
  filez.setFileName(filename);
  filez.open(QIODevice::Append);
  phdfilez<<mag.getMag().getZ()<<"\t";
  filez.close();

  //my matrix
  filename=output.getPhDFile();
  filename.append("_my.txt");
  filey.setFileName(filename);
  filey.open(QIODevice::Append);
  phdfiley<<mag.getMag().getX()<<"\t";
  filey.close();
    
  //mx matrix
  filename=output.getPhDFile();
  filename.append("_mx.txt");
  filex.setFileName(filename);
  filex.open(QIODevice::Append);
  phdfilex<<mag.getMag().getX()<<"\t";
  filex.close();
 
  return 0;
}

int PhDSave::save(Output output,QString value)
{
  QFile filex,filey,filez;
  QTextStream phdfilex(&filex);
  QTextStream phdfiley(&filey);
  QTextStream phdfilez(&filez);
  QString filename;
  QTextStream cout(stdout);
  QTextStream cin(stdin);
 
  
  //mz matrix
  filename=output.getPhDFile();
  filename.append("_mz.txt");
  filez.setFileName(filename);
  filez.open(QIODevice::Append);
  phdfilez<<value;
  filez.close();

  //my matrix
  filename=output.getPhDFile();
  filename.append("_my.txt");
  filey.setFileName(filename);
  filey.open(QIODevice::Append);
  phdfiley<<value;
  filey.close();
    
  //mx matrix
  filename=output.getPhDFile();
  filename.append("_mx.txt");
  filex.setFileName(filename);
  filex.open(QIODevice::Append);
  phdfilex<<value;
  filex.close();

  return 0;
}

int PhDSave::save(Output output,double value)
{
  QFile phdfile;
  QTextStream stream;
  QString filename;
  filename=output.getPhDFile();
  filename.append(".txt");
  phdfile.setFileName(filename);
  phdfile.open(QIODevice::Append);
  stream<<value<<"\t";
  phdfile.close();
  return 0;
}

int PhDSave::initSave(Output output)
{
  //copy Parameters file
  QString command="cp ../Parameters.cc ";
  command+=output.getParamsFile();
  system(command.toAscii().data());
  return 0;
}
